using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlServerCe;
using System.Xml;

namespace Pitoshka.DB.MSSqlCe
{
    public class MSSqlCeEngile : SqlEngine
    {
        private readonly SqlCeConnection _Connection;
        protected override DbConnection Connection
        {
            get { return _Connection; }
        }

        public MSSqlCeEngile() : this(PreferredConnString)
        {
        }
        public MSSqlCeEngile(string connectionString)
            : base(connectionString)
        {
            _Connection = new SqlCeConnection(connectionString);
        }


        public override DataTable ExecuteSelect(string sql, CommandType commandType, DBParameter[] parameters)
        {
            OpenConnection();
            SqlCeCommand sqlCommand = CreateDbCommand(sql, commandType, parameters);

            var dataAdapter = new SqlCeDataAdapter(sqlCommand);
            var dataTable = new DataTable();
            dataTable.BeginLoadData();
            dataAdapter.Fill(dataTable);
            dataTable.EndLoadData();
            dataAdapter.Dispose();
            sqlCommand.Dispose();
            CloseConnection();
            return dataTable;
        }
        public override DataSet ExecuteDataSet(string sql, CommandType commandType, DBParameter[] parameters)
        {
            OpenConnection();
            SqlCeCommand sqlCommand = CreateDbCommand(sql, commandType, parameters);

            var dataAdapter = new SqlCeDataAdapter(sqlCommand);
            var dataSet = new DataSet();
            dataAdapter.Fill(dataSet);
            dataAdapter.Dispose();
            sqlCommand.Dispose();
            CloseConnection();
            return dataSet;
        }
        public override XmlDocument ExecuteXML(string sql, CommandType commandType, DBParameter[] parameters)
        {
            throw new NotImplementedException();
        }
        public override XmlReader ExecuteXMLreader(string sql, CommandType commandType, DBParameter[] parameters)
        {
            throw new NotImplementedException();
        }
        public override int ExecuteNonQuery(string sql, CommandType commandType, DBParameter[] parameters)
        {
            OpenConnection();
            SqlCeCommand sqlCommand = CreateDbCommand(sql, commandType, parameters);

            int retval = sqlCommand.ExecuteNonQuery();
            sqlCommand.Dispose();
            CloseConnection();
            return retval;
        }
        public override object ExecuteScalar(string sql, CommandType commandType, DBParameter[] parameters)
        {
            OpenConnection();
            SqlCeCommand sqlCommand = CreateDbCommand(sql, commandType, parameters);
            object retval = sqlCommand.ExecuteScalar();
            sqlCommand.Dispose();
            CloseConnection();
            return retval;
        }
        public override DbDataReader ExecuteReader(string sql, CommandType commandType, DBParameter[] parameters)
        {
            throw new NotImplementedException();
        }

        public new SqlCeCommand CreateDbCommand(string sql, CommandType commandType, DBParameter[] parameters)
        {
            var sqlCommand = new SqlCeCommand(sql, _Connection);
            sqlCommand.CommandType = commandType;

            foreach (DBParameter parameter in parameters)
            {
                sqlCommand.Parameters.Add(new SqlCeParameter(parameter.Name, parameter.Value));
            }

            return sqlCommand;
        }
    }
}
